package com.example.rocket.config;

import com.example.rocket.producer.NbMqProducer;
import com.example.rocket.producer.OnsMqProducer;
import com.example.rocket.producer.RocketMqProducer;
import org.apache.rocketmq.client.exception.MQClientException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @description: NbMq-Producer自动注入
 * @author TAO
 * @date 2021/12/20 7:03 下午
 */

@Configuration
@EnableConfigurationProperties(NbMqProperties.class)
public class NbAutoProducerConfiguration {


    /**
     * 根据type初始化对应的Producer
     * @param properties
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "rmq.producer", name = "producerId")
    public NbMqProducer nbMqProducer(NbMqProperties properties) throws MQClientException {
        String type = properties.getType();
        if ("rocket".equals(type)){
            return new RocketMqProducer(properties);
        }else if ("ons".equals(type)){
            return new OnsMqProducer(properties);
        }else{
            throw new RuntimeException("请提供正确的MQ客户端类型 ， 如 : rocket / ons");
        }
    }


}
